package music.dao;

import music.model.MemberVO;
import music.utils.dbcp.OracleDBCP;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MemberDAO {
    private static String loginSQL = "select count(mno) as cnt from member where userid = ? and passwd = ?";
    private static String joinSQL = "insert into member(mno,userid, passwd, name, email) values(mbseq.nextval, ?,?,?,?)";
    private static String readOneSQL = "select * from member where userid = ?";
    private static String editMember = "update MEMBER set passwd=?, name=?, email=? where userid=?";
    private static String reaOneUid = "select count(uid) from member where userid=?";

    public static boolean joinMember(MemberVO mvo) {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean isJoin = false;


        try {
            conn = OracleDBCP.openDBCP();
            ps = conn.prepareStatement(joinSQL);
            ps.setString(1, mvo.getUserid());
            ps.setString(2, mvo.getPasswd());
            ps.setString(3, mvo.getName());
            ps.setString(4, mvo.getEmail());

            if (ps.executeUpdate() > 0) isJoin = true;

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            OracleDBCP.closeDBCP(conn, ps, null);
        }

        return isJoin;
    }


    public static boolean loginMember(MemberVO mvo) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        boolean isLogin = false;


        try {
            conn = OracleDBCP.openDBCP();
            ps = conn.prepareStatement(loginSQL);
            ps.setString(1, mvo.getUserid());
            ps.setString(2, mvo.getPasswd());

            rs = ps.executeQuery();
            if (rs.next()) {
                if (rs.getInt(1) > 0) isLogin = true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            OracleDBCP.closeDBCP(conn, ps, rs);
        }
        return isLogin;
    }

    public static MemberVO readOneMember(String userid) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        MemberVO mvo = null;

        try {
            conn = OracleDBCP.openDBCP();
            ps = conn.prepareStatement(readOneSQL);
            ps.setString(1, userid);
            rs = ps.executeQuery();
            while (rs.next()) {
                mvo = new MemberVO(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6));
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            OracleDBCP.closeDBCP(conn, ps, rs);
        }

        return mvo;
    }

    public static boolean editMember(MemberVO mvo) {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean isEdit = false;


        try {
            conn = OracleDBCP.openDBCP();
            ps = conn.prepareStatement(editMember);
            ps.setString(1, mvo.getPasswd());
            ps.setString(2, mvo.getName());
            ps.setString(3, mvo.getEmail());
            ps.setString(4, mvo.getUserid());

            if (ps.executeUpdate() > 0) isEdit = true;

        } catch (Exception ex) {
            System.out.println("editBoard에서 오류 발생");
            ex.printStackTrace();
        } finally {

            OracleDBCP.closeDBCP(conn, ps, null);
        }
        return isEdit;
    }

    public static int readOneUid(String uid) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;


        try {
            conn = OracleDBCP.openDBCP();
            ps = conn.prepareStatement(reaOneUid);
            ps.setString(1, uid);
            rs = ps.executeQuery();

            if( rs.next()) {
                if (Integer.parseInt(rs.getString(1)) > 0) {
                    //System.out.println(Integer.parseInt(rs.getString(1))+"몇개냐 ㅋㅋ");
                    result = 1;
                }
            }

        } catch (Exception ex) {
            System.out.println("editBoard에서 오류 발생");
            ex.printStackTrace();
        } finally {

            OracleDBCP.closeDBCP(conn, ps, null);
        }

        return result;
    }
}


